//
//  MainController.m
//  PianoAndPoemDirectSeeding
//
//  Created by 梅津山 on 2017/7/7.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "MainController.h"
#import "MainCell.h"
#import "ReleaseController.h"
#import "NowController.h"
#import <AVOSCloud.h>
#import "HomeModel.h"
#import <MJRefresh/MJRefresh.h>
#import <MBProgressHUD/MBProgressHUD.h>
#import "ImageScrollerView.h"

@interface MainController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) ImageScrollerView *scrollerV;


@end


@implementation MainController

- (void)viewDidLoad {
    [super viewDidLoad];
    _num = 10000000;
    [self dataCliced];
    
    
    
//    self.mainTableView.estimatedRowHeight = 100;
//    self.mainTableView.rowHeight = UITableViewAutomaticDimension;
    //上拉加载
        self.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self dataCliced];
        }];
    
    
}

- (void)dataCliced{
     [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AVQuery *query = [AVQuery queryWithClassName:@"Content"];
    [query orderByDescending:@"createdAt"];
    // owner 为 Pointer，指向 _User 表
    [query includeKey:@"owner"];
    // image 为 File
    [query includeKey:@"imageArr"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects) {
           [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSMutableArray *nowArr = [NSMutableArray arrayWithCapacity:objects.count];
            for (AVObject *objc in objects) {
                [self.mainTableView.mj_header endRefreshing];
                
                NSArray *fileArr = [objc valueForKey:@"imageArr"];
                AVUser *user = [objc objectForKey:@"owner"];
               
                NSString *par = [objc objectForKey:@"particulars"];
                NSString * loca = [objc objectForKey:@"location"];
                NSString *state = [objc objectForKey:@"state"];
                NSString *grade = [objc objectForKey:@"grade"];
                NSDate *createdAt = [objc objectForKey:@"createdAt"];
                NSString *image =  [user objectForKey:@"image"];
                NSString *name = user.username;
              //  NSLog(@"%@",[objc objectForKey:@"particulars"]);
                NSMutableArray *urlArr = [NSMutableArray arrayWithCapacity:fileArr.count];
                for (AVFile *file in fileArr) {
                    [urlArr addObject:file.url];
                }
                NSMutableDictionary *dictAll = [NSMutableDictionary dictionary];
                if (user) {
                    [dictAll setObject:user forKey:@"owner"];
                }
                if (grade) {
                    [dictAll setObject:grade forKey:@"grade"];
                }
                if (state) {
                    [dictAll setObject:state forKey:@"state"];
                }
                if (par) {
                    [dictAll setObject:par forKey:@"particulars"];
                }
                if (loca) {
                    [dictAll setObject:loca forKey:@"location"];
                }
                if (createdAt) {
                    [dictAll setObject:createdAt forKey:@"createdAt"];
                }
                if (urlArr) {
                    [dictAll setObject:urlArr forKey:@"imageArr"];
                }
                if (image) {
                    [dictAll setObject:image forKey:@"image"];
                }
                if (name) {
                    [dictAll setObject:name forKey:@"name"];
                }
                HomeModel *model = [[HomeModel alloc] initWithNewDict:dictAll];
                [nowArr addObject:model];
            }
            //            NSArray *data = objects[@"results"];
            self.dataArr = nowArr;
            [self.mainTableView reloadData];
        }
    }];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"";
    if (indexPath.row == _num) {
        identifier = @"MainTwoCell";
    }else {
        identifier = @"MainCell";
    }
    HomeModel *model = self.dataArr[indexPath.row];
    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.scrollerBlock = ^{
        [super.navigationController setNavigationBarHidden:YES animated:YES];
        _scrollerV = [ImageScrollerView sharedRect:self.view.frame];
        _scrollerV.mainBlock = ^{
            [super.navigationController setNavigationBarHidden:NO animated:YES];
        };
        [_scrollerV tapActionImage:model.imageArr];
        [self.view addSubview:_scrollerV];
    };
    
    if (indexPath.row == _num) {
        cell.isOk = YES;
    }else {
        cell.isOk = NO;
    }
    NSLog(@"---%@",model.name);
    cell.news = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _num) {
        _num = 10000000;
    }else {
        _num = indexPath.row;
    }
    [self.mainTableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _num) {
        NSInteger temp = 0;
        HomeModel *model = self.dataArr[indexPath.row];
        NSArray *fileArr = model.imageArr;
        if (fileArr) {
            temp = fileArr.count;
        }
        if (temp <= 3 && temp > 0) {
            return 183;
        }else if(temp == 0){
            return 100;
        }else if(temp > 3 && temp < 7){
            return 300;
        }else{
            return 400;
        }
    }else{
        return 100;
    }
}
//发布页面跳转
- (IBAction)releaseBtnClicked:(UIButton *)sender {
    UINavigationController *nav = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ReleaseController"];
    [self presentViewController:nav animated:YES completion:nil];
}

//左弹窗视图
- (IBAction)LeftBtn:(id)sender {
    NowController *now = [NowController sharedNow];
    [now leftMynow];
}
//常见问题




@end
